package zero.mybatisplus.dynamictable.param;

import com.baomidou.mybatisplus.annotation.TableName;
import zero.mybatisplus.dynamictable.annotation.DynamicTable;
import zero.mybatisplus.dynamictable.emnus.TypeEnums;

import java.util.Objects;

/**
 * @author zero
 */
public class DynamicTableParam {
    /*
     * 数据库
     */
    private String databaseName;
    /*
     * 表名
     */
    private String tableName;
    /*
     * 分表类型
     */
    private TypeEnums type;
    /*
     * 时间值
     */
    private Object dateValue;

    private DynamicTableParam() {
    }

    public static DynamicTableParam initDateValue(String databaseName, String tableName, TypeEnums type, Object dateValue) {
        DynamicTableParam dynamicTableParam = new DynamicTableParam();
        dynamicTableParam.setDatabaseName(databaseName);
        dynamicTableParam.setTableName(tableName);
        dynamicTableParam.setDateValue(dateValue);
        dynamicTableParam.setType(type);
        return dynamicTableParam;
    }

    public static DynamicTableParam initDateValue(String tableName, TypeEnums type, Object dateValue) {
        DynamicTableParam dynamicTableParam = new DynamicTableParam();
        dynamicTableParam.setTableName(tableName);
        dynamicTableParam.setDateValue(dateValue);
        dynamicTableParam.setType(type);
        return dynamicTableParam;
    }

    public static DynamicTableParam initDateValue(Class cls, Object dateValue) {
        DynamicTableParam dynamicTableParam = new DynamicTableParam();
        dynamicTableParam.setDateValue(dateValue);
        if (Objects.nonNull(cls) && cls.isAnnotationPresent(DynamicTable.class)) {
            DynamicTable dynamicTable = (DynamicTable) cls.getAnnotation(DynamicTable.class);
            TableName tableName = (TableName) cls.getAnnotation(TableName.class);
            dynamicTableParam.setDatabaseName(dynamicTable.databaseName());
            dynamicTableParam.setTableName(tableName.value());
            dynamicTableParam.setType(dynamicTable.type());
            return dynamicTableParam;
        }
        return dynamicTableParam;
    }


    public String getDatabaseName() {
        return databaseName;
    }

    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public TypeEnums getType() {
        return type;
    }

    public void setType(TypeEnums type) {
        this.type = type;
    }

    public Object getDateValue() {
        return dateValue;
    }

    public void setDateValue(Object dateValue) {
        this.dateValue = dateValue;
    }

    @Override
    public String toString() {
        return "DynamicTableParam{" +
                "databaseName='" + databaseName + '\'' +
                ", tableName='" + tableName + '\'' +
                ", type=" + type +
                ", dateValue=" + dateValue +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DynamicTableParam that = (DynamicTableParam) o;
        return Objects.equals(databaseName, that.databaseName) &&
                Objects.equals(tableName, that.tableName) &&
                type == that.type &&
                Objects.equals(dateValue, that.dateValue);
    }

    @Override
    public int hashCode() {
        return Objects.hash(databaseName, tableName, type, dateValue);
    }
}
